package routes

import (
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1/auth"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1/common"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1/driver"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1/passenger"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1/track"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1/trip"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1/user"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1/vehicle"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/http/middlewares"
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/config"
	"github.com/gofiber/fiber/v2"
)

func RegisterStaticService(app *fiber.App) {
	app.Static("/", "./public")
	app.Static("/", "./dist")
}

func RegisterApiRoutes(app *fiber.App) {

	UploadsController := new(common.UploadsController)

	signupController := new(auth.SignupController)

	verifyCodeController := new(auth.VerifyCodeController)

	loginController := new(auth.LoginController)

	// 用户
	userController := new(user.UsersController)

	// 司机
	driverController := new(user.DriversController)

	// 乘客
	passengerController := new(passenger.PassengersController)

	// 乘客乘车人
	passengerRiderController := new(passenger.RidersController)

	// 乘客黑名单
	passengerBlacklistsController := new(passenger.BlacklistsController)

	// 乘客关注的司机
	passengerUserFollowsController := new(passenger.UserFollowsController)

	// 品牌
	vehicleBrandsController := new(vehicle.VehicleBrandsController)

	// 型号
	vehicleModelsController := new(vehicle.VehicleModelsController)

	// 变速箱类型
	transmissionTypesController := new(vehicle.TransmissionTypesController)
	fuelTypesController := new(vehicle.FuelTypesController)

	// 可选路径
	tracksController := new(track.TracksController)

	// 司机车辆
	driverVehicleController := new(driver.VehiclesController)

	// 司机管理行程
	driverTripController := new(driver.TripsController)

	// 首页行程
	tripController := new(trip.TripsController)

	v1 := app.Group("/v1")
	{
		v1.Get("/", func(c *fiber.Ctx) error {
			return c.SendString("Hello, World 👋!")
		})

		// 车辆品牌
		vehicle := v1.Group("/vehicle")
		{
			vehicle.Get("/brands", vehicleBrandsController.Index)
			vehicle.Get("/models", vehicleModelsController.Index)
			vehicle.Get("/transmissions", transmissionTypesController.Index)
			vehicle.Get("/fuels", fuelTypesController.Index)
		}

		// 开通的路径
		track := v1.Group("/tracks")
		{
			track.Get("/", tracksController.Index)
		}

		// 公共
		common := v1.Group("common").Use(middlewares.Auth())
		{
			// 上传
			common.Post("upload", UploadsController.Store)
		}

		// 用户
		userGroup := v1.Group("/users").Use(middlewares.Auth())
		{
			// 用户详情
			userGroup.Get("/", userController.Show)

			// 更新用户信息
			userGroup.Put("/", userController.Update)

			// userRider := userGroup.Group("/riders")
			// {
			// 	userRider.Get("/", )
			// }
		}

		// 司机个人信息
		driverGroup := v1.Group("/driver").Use(middlewares.Auth())
		{
			// 司机添加个人信息
			driverGroup.Post("/store", driverController.Store)

			driverVehicleGroup := driverGroup.Group("vehicles").Use(middlewares.Driver())
			{
				// 司机添加车辆
				driverVehicleGroup.Post("/", driverVehicleController.Store)

				// 修改车辆信息
				driverVehicleGroup.Put("/:id", driverVehicleController.Update)

				// 车辆列表
				driverVehicleGroup.Get("/", driverVehicleController.Index)

				// 车辆信息
				driverVehicleGroup.Get("/:id", driverVehicleController.Show)

				// 删除车辆
				driverVehicleGroup.Delete("/:id", driverVehicleController.Delete)
			}

			driverTripGroup := driverGroup.Group("trips").Use(middlewares.Driver())
			{
				// 司机发布行程
				driverTripGroup.Post("/", driverTripController.Store)

				// 我发布的行程
				driverTripGroup.Get("/", driverTripController.Index)

				// 行程详情
				driverTripGroup.Get("/:id", driverTripController.Show)

				// 删除发布的行程
				driverTripGroup.Delete("/:id", driverTripController.Delete)
			}
		}

		// 乘客相关
		passengerGroup := v1.Group("/passengers").Use(middlewares.Auth())
		{
			// 乘客信息添加
			passengerGroup.Post("/store", passengerController.Store)

			// 乘车人管理
			passengerRiderGroup := passengerGroup.Group("/riders").Use(middlewares.Passenger())
			{
				// 列表
				passengerRiderGroup.Get("/", passengerRiderController.Index)

				// 详情
				passengerRiderGroup.Get("/:id", passengerRiderController.Show)

				// 添加
				passengerRiderGroup.Post("/", passengerRiderController.Store)

				// 修改
				passengerRiderGroup.Put("/:id", passengerRiderController.Update)

				// 删除
				passengerRiderGroup.Delete("/:id", passengerRiderController.Delete)
			}

			// 乘客黑名单
			passengerBlacklistGroup := passengerGroup.Group("/blacklists").Use(middlewares.Passenger())
			{
				// 列表
				passengerBlacklistGroup.Get("/", passengerBlacklistsController.Index)

				// 详情
				passengerBlacklistGroup.Get("/:id", passengerBlacklistsController.Show)

				// 添加
				passengerBlacklistGroup.Post("/", passengerBlacklistsController.Store)

				// 删除
				passengerBlacklistGroup.Delete("/:id", passengerBlacklistsController.Delete)
			}

			// 乘客的关注
			passengerUserFollowsGroup := passengerGroup.Group("/follows").Use(middlewares.Passenger())
			{
				// 关注的司机
				passengerUserFollowsGroup.Get("/", passengerUserFollowsController.Index)

				// 关注司机
				passengerUserFollowsGroup.Post("/", passengerUserFollowsController.Store)

				// 取消关注
				passengerUserFollowsGroup.Delete("/:follow_id", passengerUserFollowsController.Delete)
			}
		}

		// 行程
		tripGroup := v1.Group("/trips").Use(middlewares.Auth())
		{
			// 行程列表
			tripGroup.Get("/", tripController.Index)

			// 行程详情
			tripGroup.Get("/:id", tripController.Show)
		}

		authGroup := v1.Group("/auth")
		{
			// 微信小程序通过code置换openid，登录
			authGroup.Get("/miniprogram/login", loginController.MiniprogramLogin)

			// 刷新token
			authGroup.Post("/miniprogram/login/refresh-token", loginController.RefreshToken)

			authGroup.Post("/signup/phone/exist", signupController.IsPhoneExist)

			authGroup.Post("/signup/email/exist", signupController.IsEmailExist)

			// 图片验证码，需要加限流
			authGroup.Use(middlewares.Limiter(config.GetInt("captcha.limit"))).Post("/verify-codes/captcha", verifyCodeController.ShowCaptcha)

			// 短信验证码
			authGroup.Use(middlewares.Limiter(config.GetInt("captcha.limit"))).Post("/verify-codes/phone", verifyCodeController.SendUsingPhone)

			// 发送邮箱验证码
			authGroup.Use(middlewares.Limiter(config.GetInt("captcha.limit"))).Post("/verify-codes/email", verifyCodeController.SendUsingEmail)
		}

	}
}
